package com.mxx.common.netty.http;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.cookie.Cookie;
import io.netty.util.CharsetUtil;

import static io.netty.handler.codec.http.HttpHeaderNames.*;

/**
 * @Auther: Mr. Zeng
 * @Date: 2020/6/29 11:14
 * @Description: 暂无描述
 */

public class NettyHttpResponse extends DefaultFullHttpResponse {
    private boolean forward = false;

    protected NettyHttpResponse(HttpResponseStatus status, ByteBuf byteBuf) {
        super(HttpVersion.HTTP_1_1, status, byteBuf);
        withCors();
        headers().setInt(CONTENT_LENGTH, this.content().readableBytes());
    }

    protected NettyHttpResponse(HttpResponseStatus status) {
        super(HttpVersion.HTTP_1_1, status);
        withCors();
        headers().setInt(CONTENT_LENGTH, this.content().readableBytes());
    }

    protected NettyHttpResponse(HttpResponseStatus status, String content) {
        this(status, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8));
        withCors();
        headers().setInt(CONTENT_LENGTH, this.content().readableBytes());
    }


    public NettyHttpResponse withCors() {
        headers()
                .set(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
                .set(ACCESS_CONTROL_ALLOW_HEADERS, "*")
                .set(ACCESS_CONTROL_ALLOW_METHODS, "GET,POST,PUT,DELETE")
                .set(ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
        return this;
    }

    public boolean isSuccess() {
        return status().compareTo(HttpResponseStatus.OK) == 0;
    }

    public void addCookie(Cookie cookie) {
        headers().add(SET_COOKIE, cookie);
    }

    public NettyHttpResponse withJson() {
        headers()
                .set(CONTENT_TYPE, "application/json;charset=utf-8")
                .set(ACCEPT_CHARSET, "utf-8");
        return this;
    }

    public boolean hasContentType() {
        return headers().get(CONTENT_TYPE) != null;
    }

    public NettyHttpResponse withHtml() {
        headers()
                .set(CONTENT_TYPE, "text/html;charset=utf-8")
                .set(ACCEPT_CHARSET, "utf-8");
        return this;
    }

    public NettyHttpResponse withImage() {
        headers()
                .set(CONTENT_TYPE, "image/jpeg")
                .set(PRAGMA, "No-cache")
                .set(CACHE_CONTROL, "No-cache")
                .set(EXPIRES, 0);
        return this;
    }

    public NettyHttpResponse withRedirect(String url) {
        headers().set(ACCESS_CONTROL_ALLOW_HEADERS, "x-requested-with,content-type")
                .set(ACCESS_CONTROL_ALLOW_METHODS, "POST,GET")
                .set(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
                .set(LOCATION, url);
        return this;
    }

    public boolean isForward() {
        return forward;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append(protocolVersion().toString()).append(" ").append(status().toString()).append("\n");
        builder.append(CONTENT_TYPE).append(": ").append(headers().get(CONTENT_TYPE)).append("\n");
        builder.append(CONTENT_LENGTH).append(": ").append(headers().get(CONTENT_LENGTH)).append("\n");
        return builder.toString();
    }
}